<template>
  <div class="manage">
    <!--新增、修改表单数据弹出窗-->
    <el-dialog title="提示" :visible.sync="dialogVisible" :before-close="handleClose" width="50%">
      <el-form ref="form" :inline="true" :rules="rules" :model="form" label-width="80px">
        <el-form-item label="姓名" prop="name">
          <el-input v-model="form.name" placeholder="请输入姓名"></el-input>
        </el-form-item>
        <el-form-item label="年龄" prop="age">
          <el-input v-model="form.age" placeholder="请输入年龄"></el-input>
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择活动性别">
            <el-option label="男" :value="1"></el-option>
            <el-option label="女" :value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="出生日期" prop="birth">
          <el-date-picker v-model="form.birth" type="date" placeholder="请选择日期" value-format="yyyy-MM-DD">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="地址" prop="addr">
          <el-input v-model="form.addr" placeholder="请输入地址"></el-input>
        </el-form-item>
      </el-form>
      <!--按钮钮区分开始-->  
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </span>
      <!--按钮钮区分结束-->
    </el-dialog>
    <!-- 管理头 -->
    <div class="manage-header">
      <el-button type="primary" @click="handleAdd">+ 新增</el-button>
      <el-form :inline="true" :model="userForm">
        <el-form-item>
          <el-input type="input" v-model="userForm.name"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <!-- 表格区 -->
    <div class="common-table">
    <!-- 数据表格区 -->
      <el-table height="90%" stripe :data="tableData" style="width: 100%">
        <el-table-column prop="name" label="姓名" width="180">
        </el-table-column>
        <el-table-column prop="age" label="年龄">
        </el-table-column>
        <el-table-column prop="sex" label="性别">
          <template slot-scope="scope">
            <span style="margin-left: 10px">{{ scope.row.sex === 1 ? '男' : '女' }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="birth" label="出生日期">
        </el-table-column>
        <el-table-column prop="addr" label="地址">
        </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <el-button size="mini" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" type="danger" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <div class="pager">
        <el-pagination layout="prev, pager, next" :total="total" @current-change="handleCurrentChange">
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import { addUser, delUser, editUser, getUser } from '../api'

export default {
  name: "Home",
  data() {
    return {
      dialogVisible: false,
      form: {
        name: '',
        age: '',
        sex: '',
        birth: '',
        addr: '',
      },
      rules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' },
        ],
        age: [
          { required: true, message: '请输入年龄', trigger: 'blur' },
        ],
        sex: [
          { required: true, message: '请选择性别', trigger: 'blur' },
        ],
        birth: [
          { required: true, message: '请选择出生日期', trigger: 'blur' },
        ],
        addr: [
          { required: true, message: '请输入地址', trigger: 'blur' },
        ],
      },
      tableData: [],
      modalType: 0,// 0表示新增的弹框，1表示编辑的弹框
      total: 0,//当前数据总条数
      pageData: {
        page: 1,
        limit: 10
      },
      userForm: {
        name: ''
      },
    }
  },
  methods: {
    // 用户提交表单
    submit() {
      this.$refs.form.validate((valid) => {
        if (valid) {
          // 后续操作
          if (this.modalType === 0) {
            addUser(this.form).then(() => {
              // 重新获取列表接口
              this.getList();
            })
          } else {
            editUser(this.form).then(() => {
              this.getList();
            });
          }
          console.log(this.form);
          // 关闭弹窗
          this.dialogVisible = false;
          // 表单内容清空
          this.$refs.form.resetFields();
        }
      })
    },
    handleClose() {
      // 弹框关闭前情况数据
      this.$refs.form.resetFields();
      this.dialogVisible = false;
    },
    cancel() {
      this.handleClose();
    },
    handleEdit(row) {
      this.modalType = 1;
      this.dialogVisible = true;
      // 注意，需要对数据进行深拷贝
      this.form = JSON.parse(JSON.stringify(row));
    },
    handleDelete(row) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        console.log('TTT row.id', row.id);
        delUser({ id: row.id }).then(() => {
          this.$message({
            type: 'success',
            message: '删除成功!'
          });
          // 重新获取列表
          this.getList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    // 获取列表
    // 获取列表
    getList() {
      getUser({ params: { ...this.userForm, ...this.pageData } }).then(({ data }) => {
        // console.log('TTT',data);
        this.tableData = data.list;
        this.total = data.count || 0;
      })
    },
    handleCurrentChange(val) {
      this.pageData.page = val;
      this.getList();
    },
    handleAdd() {
      this.modalType = 0;
      this.dialogVisible = true;
    },
    onSubmit() {
      this.getList();
    },
  },
  mounted() {
    this.getList();
  }
}
</script>

<style lang="less" scoped>
.manage {
  height: 90%;

  .manage-header {
    display: flex;
    justify-content: space-between;
    align-items: baseline;
  }

  .common-table {
    position: relative;
    height: calc(100% - 62px);

    .pager {
      position: absolute;
      bottom: 0;
      right: 20px;
    }
  }
}
</style>
